package com.ruoyi.web.controller.warehouse;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.MaterialInfo;
import com.ruoyi.system.service.IMaterialInfoService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物料信息Controller
 *
 * @author tanshen
 * @date 2023-08-21
 */
@Api("物料信息")
@Controller
@RequestMapping("/warehouse/materialInfo")
public class MaterialInfoController extends BaseController {
    private final String prefix = "warehouse/materialInfo";

    @Autowired
    private IMaterialInfoService materialInfoService;

    @RequiresPermissions("warehouse:materialInfo:view")
    @GetMapping()
    public String materialInfo() {
        return prefix + "/materialInfo";
    }

    @GetMapping(value = "/getMaterials")
    @ResponseBody
    public Map<String, Object> list(@RequestParam("materialId") String materialId,
                                    @RequestParam(value = "supplierId", required = false) String supplierId) {
        startPage();
        List<MaterialInfo> list = materialInfoService.selectMaterialList(materialId, supplierId);
        if (CollectionUtils.isEmpty(list)) {
            return new HashMap<>();
        }

        Map<String, Object> result = new HashMap<>(2);
        result.put("count", list.size());
        result.put("value", list);
        return result;
    }

    /**
     * 查询物料信息列表
     */
    @RequiresPermissions("warehouse:materialInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MaterialInfo materialInfo) {
        startPage();
        List<MaterialInfo> list = materialInfoService.selectMaterialInfoList(materialInfo);
        return getDataTable(list);
    }

    /**
     * 导出物料信息列表
     */
    @RequiresPermissions("warehouse:materialInfo:export")
    @Log(title = "物料信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MaterialInfo materialInfo) {
        List<MaterialInfo> list = materialInfoService.selectMaterialInfoList(materialInfo);
        ExcelUtil<MaterialInfo> util = new ExcelUtil<MaterialInfo>(MaterialInfo.class);
        return util.exportExcel(list, "物料信息数据");
    }

    /**
     * 新增物料信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存物料信息
     */
    @RequiresPermissions("warehouse:materialInfo:add")
    @Log(title = "物料信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MaterialInfo materialInfo) {
        return toAjax(materialInfoService.insertMaterialInfo(materialInfo));
    }

    /**
     * 修改物料信息
     */
    @RequiresPermissions("warehouse:materialInfo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        MaterialInfo materialInfo = materialInfoService.selectMaterialInfoById(id);
        mmap.put("materialInfo", materialInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存物料信息
     */
    @RequiresPermissions("warehouse:materialInfo:edit")
    @Log(title = "物料信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MaterialInfo materialInfo) {
        return toAjax(materialInfoService.updateMaterialInfo(materialInfo));
    }

    /**
     * 删除物料信息
     */
    @RequiresPermissions("warehouse:materialInfo:remove")
    @Log(title = "物料信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(materialInfoService.deleteMaterialInfoByIds(ids));
    }
}
